<template>
  <v-layout type="console">
    <div class="coupon-layout">
      <p class="hint"><i class="el-icon-warning-outline"></i> 使用SSH公钥</p>
      <p class="cost-title">您当前的SSH KEY数:{{tableData.length}}</p>
      <div class="coupon-table">
        <el-table :data="tableData" :show-header="false" style="width: 100%">
          <el-table-column prop="name" label="name" :width="300">
            <template slot-scope="{ row }">
              <el-link type="primary" :underline="false" @click="showDetail(row)">{{row.name}}</el-link>
            </template>
          </el-table-column>
          <el-table-column prop="fingerprint" label="fingerprint">
            <template slot-scope="{row}">
              <div class="keys-gray ">{{row.fingerprint}}</div>
            </template>
          </el-table-column>
          <el-table-column prop="createTime" :width="200" label="创建时间"></el-table-column>
          <el-table-column prop="startTime" :width="200" label="操作">
            <template slot-scope="{ row }">
              <el-button type="text" @click="delKey(row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

      </div>
      <p class="cost-title cost-add">添加公钥</p>
      <el-form ref="form" :model="formData" :rules="rules" label-width="120px" label-position="left">
        <el-row>
          <el-col :span="12">
            <el-form-item label="标题:" prop="name">
              <el-input v-model="formData.name" :disabled="disabledEditName" placeholder="从用户公钥读取"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="公钥:" prop="publicKey">
              <el-input type="textarea" v-model="formData.publicKey" placeholder="请编辑公钥" maxlength="1000" show-word-limit :rows="8"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item>
          <el-button type="primary" @click="submit">确定</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-dialog :visible.sync="showVerify" title="账户安全验证" width="800px">
      <el-form label-width="80px">
        <span class="title m-l">你正在进行SSH公钥添加操作，请验证你的身份</span>
        <h4 class="m-l">密码验证</h4>
        <el-form-item label="" prop="name">
          <el-input type="password" v-model="password" placeholder="请输入OF智能云系统密码" autoComplete="off" />
        </el-form-item>
        <el-form-item label=" ">
          <div class="btn-box">
            <v-btn type="info" @click="cancelHandler">取消</v-btn>
            <v-btn type="primary" @click="verifyPassword">确定</v-btn>
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-dialog :visible.sync="showView" title="查看公钥信息" width="800px">
      <el-form label-width="120px">
        <el-form-item label="公钥标题">
          {{detail.name}}
        </el-form-item>
        <el-form-item label="安全链接">
          {{detail.fingerprint}}
        </el-form-item>
        <el-form-item label="公钥 ">
          <el-input type="textarea" v-model="detail.publicKey" readonly :rows="8"></el-input>
        </el-form-item>
      </el-form>
    </el-dialog>
  </v-layout>
</template>
  
<script>

export default {
  data () {
    return {
      showVerify: false,
      showView: false,
      disabledEditName: true,
      detail: {},
      formData: {
        name: '',
        publicKey: ''
      },
      password: '',// verify password
      tableData: [],
      rules: {
        name: [
          { required: true, message: "请输入标题" },
          { required: true, min: 1, max: 100, message: "长度在 1 到 100 个字符" },
        ],
        publicKey: [
          { required: true, message: "请输入publicKey" },
          { required: true, min: 1, max: 1000, message: "长度在 1 到 1000 个字符" }
        ]
      }
    }
  },
  components: {

  },
  methods: {
    verifyPassword() {

    },
    async submit () {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          this.password = '';
          this.showVerify = true;
        }
      })
    },
    cancelHandler () {
      this.password = '';
      this.showVerify = false;
    },
  },
  created () {

  },
  watch: {
    "formData.publicKey": function (val) {
      if (val) {
        let arr = val.split(" ");
        if (arr.length === 3) {
          if (!this.formData.name) { this.formData.name = arr[2]; }
          else
            this.disabledEditName = false;
        }
        else if (arr.length > 3) {
          if (!this.formData.name) { this.formData.name = arr.slice(2).join(" "); }
          this.disabledEditName = true;
        }
        else {
          this.disabledEditName = false;
        }
      }
      else {
        this.formData.name = '';
      }
    }
  },
  mounted () {
    // this.getData()
  },
  destroyed () { },
};
</script>
<style lang="less" scoped>
.coupon-layout {
  background: #fff;
  box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.1);
  border-radius: 2px;
  min-height: 100%;
}
.hint {
  height: 32px;
  background: #fef4e8;
  box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.1);
  border-radius: 2px;
  padding: 0 14px;
  border: 1px solid #fed6a7;
  box-sizing: border-box;
  line-height: 30px;
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #666666;
}
.cost-title {
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #303133;
  padding: 24px 0 12px 16px;
}
.cost-add {
  margin-top: 32px;
  font-weight: 600;
  line-height: 20px;
}
.table-nav-box {
  margin: 0 24px;
  border-bottom: 1px solid #e4e7ed;
}
.table-nav {
  overflow: hidden;
  padding-top: 6px;
  float: left;
  li {
    float: left;
    margin-right: 28px;
    padding: 0 3px 6px;
    cursor: pointer;
    border-bottom: 2px solid #fff;
  }
  li:hover,
  .active {
    color: #1e2480;
    box-sizing: border-box;
    border-bottom: 2px solid #1e2480;
  }
}
.section-time {
  float: right;
  .search-name {
    width: 200px;
    float: left;
    margin-right: 10px;
  }
}
.coupon-table {
  margin: 0 16px;
  box-sizing: border-box;
  .keys-gray {
    background: #f3f3f3;
    border-radius: 4px;
    line-height: 28px;
    display: inline-block;
    padding: 5px 10px;
  }
}
/deep/.el-form-item__label {
  padding-left: 16px;
}
/deep/.el-table__row {
  border-bottom: 2px;
}
.page-nb {
  overflow: hidden;
  padding: 0 24px;
  height: 56px;
  background: rgba(133, 144, 166, 0.08);
  border-radius: 4px;
  .page-nb-all {
    float: left;
    line-height: 56px;
    span {
      color: #4285f4;
    }
  }
  .page-nb-pagination {
    float: right;
    padding-top: 13px;
  }
}
.m-l {
  margin-left: 80px;
}
.title {
  margin-bottom: 27px;
  display: block;
  font-size: 14px;
}
.btn-box {
  float: right;
}
</style>